package com.filebackup.struts.actions;

import java.io.File;

import com.filebackup.business.LocalFileService;
import com.filebackup.business.LocalFolderService;
import com.filebackup.util.RootPathUtil;
import com.filebackup.vo.impl.UserVo;

public class ModifyFileNameAction extends FilebackupSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int type;
	private int id;
	private String newName;
	private LocalFileService localFileService;
	private LocalFolderService localFolderService;
	
	public String execute(){
		System.out.println("change name:------------------");
		System.out.println(newName);
		System.out.println(id);
		System.out.println(type);
		
		String path="";
		UserVo userVo=null;
		if(type==1){
			userVo=this.getLocalFolderService().findOwnerById(id);
			path=RootPathUtil.ROOT_PATH + userVo.getName() + "\\"+this.getLocalFolderService().returnPath(id);
			changeName(path);
			this.getLocalFolderService().changeFolderName(id, newName);
		} else if(type==2){
			userVo=this.getLocalFileService().findOwnerById(id);
			path=RootPathUtil.ROOT_PATH + userVo.getName() + "\\"+this.getLocalFileService().returnPath(id);
			changeName(path);
			this.getLocalFileService().changeFileName(id, newName);
		}
		return null;
	}
	
	public void changeName(String path){
		File file=new File(path);
		String parent=file.getParent();
		File newFile=new File(parent+File.separator+this.getNewName());
		boolean isModified=file.renameTo(newFile);
		System.out.println(isModified);
	}
	public void setType(int type) {
		this.type = type;
	}
	public int getType() {
		return type;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getId() {
		return id;
	}
	public void setNewName(String newName) {
		this.newName = newName;
	}
	public String getNewName() {
		return newName;
	}
	public void setLocalFileService(LocalFileService localFileService) {
		this.localFileService = localFileService;
	}
	public LocalFileService getLocalFileService() {
		return localFileService;
	}
	public void setLocalFolderService(LocalFolderService localFolderService) {
		this.localFolderService = localFolderService;
	}
	public LocalFolderService getLocalFolderService() {
		return localFolderService;
	}

}
